package main

import (
	"sync"
	"fmt"
	"math/rand"
	"time"
)

var(
	wg       sync.WaitGroup
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func main(){

	chanints := make(chan int)

	wg.Add(2)
	go playBall("许辉", chanints)
	go playBall("爸爸", chanints)

	// 发球
	chanints <- 1
	wg.Wait()

}

func playBall(name string,chanints chan int){
	defer wg.Done()
	for {
		 ball,ok := <- chanints;
		if !ok{
			fmt.Printf("%s, 你赢了。\n",name)
			return
		}
		// 选随机数
		n := rand.Intn(100)
		fmt.Printf("当前的随机数，%d \n",n)
		if n%11 == 0{
			fmt.Printf("%s,没接到球 \n",name)
			close(chanints)
			return
		}
		// 显示击球数
		fmt.Printf("%s Hit %d \n",name,ball)
		ball++
		chanints <- ball
	}
}
